var mockupbuild = require("../mockup");
var path = require('path');
var basename = path.basename(__filename, ".js");
var Mock = require('mockjs');
var Chance = require('chance');
var Random = Mock.Random;
var chance = new Chance();

chance.mixin({
    'pengyouquanuser': function () {
        return this.pickone([Random.cname(), Random.name()]);
    },
    'pengyouquansentence': function() {
        return this.pickone([Random.csentence(), Random.sentence()]);
    },
    'pengyouquancommoent': function() {
        let self = this;
        return self.pengyouquansentence();
    }
});

let components = {
    'link': {
        'avatar': function() {
            return chance.pickone(["./global/assets/images/global_icon_link.png", "./global/assets/images/global_icon_location.png"]);
        },
        'content': function() {
            return chance.pengyouquansentence();
        }
    },
    'text': {
        'content': function() {
            return chance.pengyouquansentence();
        }
    },
    'gallery|1-9': [{
        'content': function() {
            return chance.pickone(["https://static.zhilizhili.com/static/3828461873613813290.jpg"]);
        }
    }],
    'videos|1-3': [{
        'content': function() {
            return chance.pickone(["https://static.zhilizhili.com/static/3828461873613813290.jpg"]);
        }
    }],    
    'location': {
        'content': function() {
            return Random.cname();
        }
    },
    'zan|1-5': [{
        'content': function() {
            return chance.pengyouquanuser();
        }
    }],
    'comments': [{
        'content': function() {
            return chance.pengyouquancommoent();
        },
        'name': function() {
            return  `${chance.pengyouquanuser()}评论${chance.pengyouquanuser()}`
        }
    }]
};

Function.prototype.auiClone = function() {
    var that = this;
    var temp = function temporary() { return that.apply(this, arguments); };
    for(var key in this) {
        if (this.hasOwnProperty(key)) {
            temp[key] = this[key];
        }
    }
    return temp;
};

function chooseone(obj, searched, findarr) {
    var isContainInSearched = findarr.every(function(v) {
        return searched.includes(v);
    });
    if (isContainInSearched) {
        let servied = chance.pickone(findarr);
        findarr.forEach(function(a) {
            if (a !== servied) {
                delete obj[a];
            }
        });
    }
    return isContainInSearched;
}

Mock.Handler._object = Mock.Handler.object.auiClone();
Mock.Handler.object = function(options) {
    let ret = null;
    if (options.name.indexOf("body") > -1) {
        ret = Mock.Handler._object(options);
        let keys = Object.keys(ret);

        // 处理text link
        if (keys.indexOf("text") > -1 || keys.indexOf("link") > -1) {
            if (keys.indexOf("text") > -1 && keys.indexOf("link") > -1) {
                let deleted = chance.pickone(["text", "link"]);
                delete ret[deleted];
            }               
        } else {
            ret["text"] = {
                content: chance.pengyouquansentence()
            }
        }
       
        chooseone(ret, keys, ["gallery", "videos"]);
    } else { 
        ret = Mock.Handler._object(options);
    }
    return ret;
}


let template = {
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    'list|15-50': [{
        // 属性 id 是一个自增数，起始值为 1，每次增 1
        'id|+1': 1,
        'name': function() {
            return Random.cname();
        },
        'avatar': function() {
            return chance.pickone(["https://static.zhilizhili.com/static/3828461873613813290.jpg"]);
        },
        'create_time': function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        },
        'body|1-7': components
    }]
};

mockupbuild(template, `./data/${basename}.json`);